package xyz.labmem.app.dialog

import javafx.beans.property.SimpleDoubleProperty
import javafx.event.EventHandler
import javafx.geometry.Insets
import javafx.geometry.Pos
import javafx.scene.Cursor
import javafx.scene.Scene
import javafx.scene.control.ScrollPane
import javafx.scene.image.ImageView
import javafx.scene.layout.*
import javafx.scene.paint.Color
import javafx.scene.shape.Polygon
import javafx.stage.Modality
import javafx.stage.StageStyle
import xyz.labmem.app.config.AppConfig.mouseImage
import xyz.labmem.app.config.AppConfig.uiImage
import xyz.labmem.app.unit.BaseStage
import xyz.labmem.app.unit.LabelUnit
import xyz.labmem.app.util.ScreenType
import xyz.labmem.app.util.dragWin
import xyz.labmem.app.util.mouseClickedPoll
import xyz.labmem.util.viewRun


/**
 * @author       : ltty
 * @description  :
 * @createDate   : 2024/10/15
 */
class FormDialog(private val parent: BaseStage) : BaseStage(StackPane()) {

    private val form = VBox()

    private val btnView = VBox()

    private val title = LabelUnit(
        "弹窗",
        50.0,
        oColor = Color.web("#ffed07"),
        rotateE = 5.0,
        rotateS = .0,
        clearance = 15.0
    )

    companion object {
        private var self: FormDialog? = null
        fun setCursor(cursor: Cursor?) {
            self?.scene?.cursor = cursor
        }

        fun cursorBack() {
            self?.scene?.cursor = mouseImage["m1"]
        }
    }

    init {
        initOwner(parent)
        self = this
        val xc = SimpleDoubleProperty()
        xc.bind(root.prefWidthProperty().subtract(200))
        dragWin(title, xc = xc)
        initModality(Modality.WINDOW_MODAL)
        initStyle(StageStyle.TRANSPARENT)
        scene.cursor = mouseImage["m1"]
        scene.fill = null
        root.setPrefSize(ScreenType.getWidth() * .6, ScreenType.getHeight() * .8)
        root.style = """
                       -fx-background-color: black;
                       -fx-border-width: 3;
                       -fx-border-color: linear-gradient(to top, #fdea99, #cf926d); 
                       -fx-padding: 10;
            """.trimIndent()
        val imageView = ImageView(uiImage["default"])
        imageView.isPreserveRatio = true
        imageView.opacity = .3
        imageView.fitHeightProperty().bind(root.prefHeightProperty())
        val view = HBox(ScrollPane(form).apply {
            prefWidthProperty().bind(root.prefWidthProperty().subtract(200))
            prefHeightProperty().bind(root.prefHeightProperty())
            isFitToWidth = true
            hbarPolicy = ScrollPane.ScrollBarPolicy.NEVER
//            style = "-fx-border-color: red; -fx-border-width: 1px;"
        })
        root.children.addAll(imageView, title, view)
        view.children.add(VBox().apply {
            prefWidth = 200.0
            prefHeight = root.prefHeight
            children.add(title)
            children.add(Pane().apply { VBox.setVgrow(this, Priority.ALWAYS) })
            children.add(btnView)
        })
    }

    fun run(item: FormInterFace) {
        root.setPrefSize(item.formWidth, item.formHeight)
        y = parent.y + (ScreenType.getHeight() - root.prefHeight) / 2
        x = parent.x + (ScreenType.getWidth() - root.prefWidth) / 2
        form.alignment = item.alignment
        form.children.setAll(item.intoForm)
        item.closeForm = { viewRun { close() } }
        btnView.children.setAll(item.intoBtns)
        btnView.children.add(DialogBtn("取消") {
            close()
        }.apply {
            VBox.setMargin(this, Insets(0.0, 5.0, 20.0, 0.0))
        })
        title.textProperty.value = item.title
        show()
    }
}

class DialogBtn(text: String, color: Color = Color.WHITE, scene: Scene? = null, checkFun: () -> Unit) : Pane() {
    private val background1: Polygon
    private val background2: Polygon
    private val textUnit: LabelUnit

    init {
        prefHeight = 50.0
        prefWidth = 195.0
        val sp = StackPane().apply {
            prefHeight = 50.0
            prefWidth = 195.0
            alignment = Pos.CENTER
            background1 = Polygon().apply {
                fill = Color.web("#ff0000")
                points.setAll(
                    .0, 50.0,
                    180.0, 15.0,
                    190.0, 40.0,
                    .0, 50.0,
                )
            }
            background1.isVisible = false
            background2 = Polygon().apply {
                fill = color
                points.setAll(
                    .0, 50.0,
                    50.0, 30.0,
                    53.0, 33.0,
                    160.0, 5.0,
                    170.0, 30.0,
                    .0, 50.0,
                )
            }
            children.add(AnchorPane(background1))
            children.add(AnchorPane(background2))
            textUnit = LabelUnit(text, 28.0)
            children.add(textUnit)
            onMouseEntered = EventHandler {
                background1.isVisible = true
                scene?.cursor = mouseImage["m6"]
            }
            onMouseExited = EventHandler {
                background1.isVisible = false
                scene?.cursor = mouseImage["m1"]
            }
            onMouseClicked = mouseClickedPoll {
                textUnit.refreshText()
                checkFun()
            }
        }
        children.add(sp)
    }

}